(function(){
	require(['jquery', 'navigation', 'box', 'bootstrap', 'validator', 'fileinput'], function(jquery, navigation, box) {
		var resourceArr = orange.getAppContext('RESOURCE'), len = resourceArr.length || 0,
		i = 0, j = 0, k = 0, reObj = {}, pid="", newResource = [], newLen = 0, tempArr = [];
		for(i=0; i<len; i++) {
			reObj = resourceArr[i];
			pid = reObj.pid;
			if(pid == "" || pid == null) {
				newResource.push(reObj);
			}
		}
		
		newLen = newResource.length || 0;
		for(j=0; j<len; j++ ) {
			reObj = resourceArr[j];
			pid = reObj.pid;
			for(k=0; k<newLen; k++) {
				if(reObj.pid == newResource[k].id){
					if(newResource[k]['children']) {
						newResource[k]['children'].push(reObj);
					} else {
						newResource[k]['children'] = [reObj];
					}
				}
			}
		}
		
		$.ajax({
			url: "notice/retrieveNoReadNoticeByOrgId.service",
			type: "post",
			success: function(data) {
				if(data && data.success) {
					if(data.data > 0){
						$('.user-header').find('.fa-bell').addClass('blink');
						$('.user-header').find('.fa-bell').attr('title','您有'+ data.data +'条未读公告！');
					}else if(data.data == 0){
						$('.user-header').find('.fa-bell').removeClass('blink');
						$('.user-header').find('.fa-bell').attr('title','暂时没有公告！');
					}
				}
			}
		});
		
		var opts = {
				type: 'ICON',
				position: 'LEFT',
				field: {title: 'name', icon: 'icontext', url: 'url'},
				data: newResource,
				selected: '0',
				onSelect: function(item) {
					var children = item.children || null;
					if(children != null) {
						$('#main_left').data('resource', children);
					} else {
						$('#main_left').data('resource', null);
					}
					var url = item.url, selector = '#subMain', main_center ='';
					if($('#main_center')) {
						$('#main_center').empty();
						main_center = 'main_center';
					}
					orange.loadPage({url: url, target: main_center, selector: selector, success: function(response) {
						console.log('页面' + item.title +'加载成功');
					}});
				},
				onLoaded: function() {
					console.log('navigation组件创建完成');
				}
			};
		var overallNav = navigation.create('#main_left', opts);
		//通告
		$('#annunciateTip').bind('click', {'nav': overallNav}, handleNotice);
		//退出
		$('#logout').click(function(evt) {
			orange.stop();
		});
		//设置用户名
		setUsername();
		
		//修改密码弹出框
		$('#changePassword').bind('click', function(evt) {
			var modalObj = box.loadUpdateModal('test', "修改密码"),
				modalId = modalObj.modalId, modalBodyId = modalObj.modalBodyId, btnSure = modalObj.btnSure;
	    	var updateUrl = "system/getPage.service?path=main/changePassword",
	    	 	formId2 = 'change_password_form',
	    		_target = modalBodyId, _selector= "#changePassword";
	    	orange.loadPage({url: updateUrl, target: _target, selector: _selector, success: function(response){
	    		var modal = $('#' + modalId), 
	    			btn_sure = $('#' + btnSure);
	    		modal.modal({backdrop: "static"});
	    		
	    		btn_sure.unbind('click').bind('click', function() {
	    			$('#change_password_form').data('bootstrapValidator').validate();
	    			var jsonData = {}, flag = $('#change_password_form').data('bootstrapValidator').isValid();
	    			if(flag) {
	    				btn_sure.attr('disable', true);
	    				jsonData= $('#'+formId2).form('getFormData', {id:formId2});
	    				jsonData["username"] = orange.getAppContext('USER')['username'];
	    				$.ajax({
	    					/*修改密码*/
	    					url: "user/resetUserPasswordOldToNew.service",
	    					type: "post",
	    					data: jsonData,
	    					success: function(data) {
	    						var modalObj = box.tipModal('test'),
	    			        	modalId = modalObj.modalId, modalBodyId = modalObj.modalBodyId;
	    			        	var tip_modal = $('#' + modalId);
	    						if(data && data.success) {
	    							/*修改成功*/
	    							modal.modal('hide');
	    							$('#' + modalBodyId).html('密码修改成功。。。。');
	    							tip_modal.modal();
	    							/*表单重置*/
	    							$('#'+formId2).form('reset', {id:formId2});
	    						} else {
	    							modal.modal('hide');
	    							if(data.errorCode == "1002" || data.errorCode == "3001"){
	    				        		$('#' + modalBodyId).html('密码修改失败。。。。');
	    				        	}else if(data.erroeCode == "2003"){
	    				        		$('#' + modalBodyId).html('密码修改失败，该条数据已存在。。。。');
	    				        	}else{
	    				        		$('#' + modalBodyId).html('密码修改失败，网络异常。。。。');
	    				        	}
	    				        	tip_modal.modal();
	    	        			}
	    					},
	    	    			error: function() {},
	    	        		complete : function() {
	    	        			btn_sure.attr('disable', false);
	    	        		}
	    				});
	    			}
	    		});
	    	}});
		});

		function handleNotice(evt) {
			var nav = evt.data.nav;
			var len = $('#main_left').find('ul').children().length;
			nav.selectItem(len-1);
		}
		function setUsername() {
			$('#username').attr('title', orange.getAppContext('USER')['username']);
		}
		function bindLogout() {
			$('#logout').bind('click', function(evt) {
				orange.stop();
			});
		}
		
	});
})();